package com.xlf.common;

import com.jfinal.config.Constants;
import com.jfinal.config.Handlers;
import com.jfinal.config.Interceptors;
import com.jfinal.config.JFinalConfig;
import com.jfinal.config.Plugins;
import com.jfinal.config.Routes;
import com.jfinal.core.JFinal;
import com.jfinal.ext.interceptor.SessionInViewInterceptor;
import com.jfinal.plugin.activerecord.ActiveRecordPlugin;
import com.jfinal.plugin.c3p0.C3p0Plugin;
import com.jfinal.render.ViewType;
import com.xlf.common.controller.CatalogController;
import com.xlf.common.controller.DesignerController;
import com.xlf.common.controller.EmailController;
import com.xlf.common.controller.FactoryController;
import com.xlf.common.controller.OrderController;
import com.xlf.common.controller.ProductController;
import com.xlf.common.controller.UserController;
import com.xlf.common.model.Catalog;
import com.xlf.common.model.Email;
import com.xlf.common.model.Factory;
import com.xlf.common.model.Order;
import com.xlf.common.model.OrderCatalog;
import com.xlf.common.model.OrderItem;
import com.xlf.common.model.Product;
import com.xlf.common.model.User;
import com.xlf.util.Path;


public class CommonConfig extends JFinalConfig {

	@Override
	public void configConstant(Constants me) {
		loadPropertyFile("config.properties"); // 加载少量必要配置，随后可用getProperty(...)获取值
		me.setDevMode(getPropertyToBoolean("devMode", false));
		me.setViewType(ViewType.JSP); // 设置视图类型为Jsp，否则默认为FreeMarker
		me.setBaseViewPath("/WEB-INF/view");
		try {
			me.setUploadedFileSaveDirectory(Path.APP_PATH + "upload");
		} catch (Exception e) {
			e.printStackTrace();
		}

	}

	@Override
	public void configRoute(Routes me) {
		me.add("/product", ProductController.class);
		me.add("/order", OrderController.class);
		me.add("/catalog", CatalogController.class);
		me.add("/factory", FactoryController.class);
		me.add("/user", UserController.class);
		me.add("/designer", DesignerController.class);
		me.add("/email", EmailController.class);

	}

	@Override
	public void configPlugin(Plugins me) {
		// 配置C3p0数据库连接池插件
		C3p0Plugin c3p0Plugin = new C3p0Plugin(getProperty("jdbcUrl"),
				getProperty("user"), getProperty("password").trim());
		me.add(c3p0Plugin);

		// 配置ActiveRecord插件
		ActiveRecordPlugin arp = new ActiveRecordPlugin(c3p0Plugin);
		me.add(arp);
		//arp.addMapping("blog", Blog.class); // 映射blog 表到 Blog模型
		// by zyk 第二个参数增加主键设置，否则更新会报错。。。调试了半天，擦。
		arp.addMapping("product","p_id", Product.class); // 映射product 表到 Product模型
		arp.addMapping("orderinfo","o_id", Order.class);
		arp.addMapping("ordercatalog","oc_id", OrderCatalog.class);
		arp.addMapping("orderitem","i_id", OrderItem.class);
		arp.addMapping("catalog","c_id", Catalog.class);
		arp.addMapping("factory","f_id", Factory.class);
		arp.addMapping("user","u_id", User.class);
		arp.addMapping("email","e_id", Email.class);
		
		//arp.addMapping("test", Test.class); // 映射test 表到 Test模型

	}
	
	@Override
	public void afterJFinalStart() {
		System.out.println("jfinal 启动后自动调用");
		super.afterJFinalStart();
	}
	@Override
	public void beforeJFinalStop() {
		System.out.println("jfinal 停止前自动调用");
		super.beforeJFinalStop();
	}
	public static void main(String[] args) {
		JFinal.start("WebRoot", 5005, "/", 5);
	}

	@Override
	public void configInterceptor(Interceptors me) {
		me.add(new SessionInViewInterceptor());
	}

	@Override
	public void configHandler(Handlers me) {
		
	}
}